package com.hr.hrto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hr.hrto.common.CustomException;
import com.hr.hrto.common.R;
import com.hr.hrto.domain.Category;
import com.hr.hrto.domain.Dish;
import com.hr.hrto.domain.Setmeal;
import com.hr.hrto.mapper.CategoryMapper;
import com.hr.hrto.service.CategoryService;
import com.hr.hrto.service.DishService;
import com.hr.hrto.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 黄瑞
 * @Date: 2022/07/06/21:15
 * @Description:
 * @version:1.0
 */
@Service
public class CategoryServiceimpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetMealService setMealService;
    @Override
    /**
     * 根据id删除分类，删除之前需要判断
     */
    public void remove(Long ids) {
        //添加查询条件，根据分类id来查询
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper =new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联菜品，如果已经关联，抛出一个业务异常
        if (count1>0){
            //已经关联菜品，抛出一个业务异常
            throw new CustomException("当前分类下已经关联菜品，不能删除");
        }
        //查询当前分类是否关联套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,ids);
        int count2 = setMealService.count(setmealLambdaQueryWrapper);
        if (count2>0){
            //已经关联套餐，抛出一个业务异常
            throw new CustomException("当前分类下已经关联套餐，不能删除");
        }
        //正常删除
        else {
            super.removeById(ids);
        }

    }
}
